package pers.lan.stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/11/23 2:37 PM
 */
public class LC_880 {

    public static void main(String[] args) {
        System.out.println(new LC_880().decodeAtIndex("y959q969u3hb22odq595", 222280369));
    }

    public String decodeAtIndex(String S, int K) {
        long size = 0;
        int N = S.length();

        // Find size = length of decoded string
        for (int i = 0; i < N; ++i) {
            char c = S.charAt(i);
            if (Character.isDigit(c)) {
                size *= c - '0';
            } else {
                size++;
            }
        }

        for (int i = N - 1; i >= 0; --i) {
            char c = S.charAt(i);
            K %= size;
            if (K == 0 && Character.isLetter(c)) {
                return Character.toString(c);
            }

            if (Character.isDigit(c)) {
                size /= c - '0';
            } else {
                size--;
            }
        }

        throw null;

    }

    private boolean append(StringBuilder sb, int k, CharSequence c) {
        sb.append(c);
        return sb.length() >= k;
    }
}
